package order.service.impl;

import feign.AccountFeign;
import feign.ProductFeign;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import order.entity.Order;
import order.mapper.OrderMapper;
import order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lh
 * @since 2020-08-08
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private AccountFeign accountFeign;

    @Autowired
    private ProductFeign productFeign;

    @GlobalTransactional(name = "TX_ORDER_CREATE")
    @Override
    public void createOrder(Order order) {
        save(order);
        log.info("order xid is:{}", RootContext.getXID());
        productFeign.deduct(order.getProductCode(), order.getCount());
        accountFeign.reduce(order.getAccountCode(), order.getAmount());
    }
}
